﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using PIT.Labs.ProjectRepository.Owl2DotNet;

namespace PIT.Labs.ProjectRepository.OntologyFeature
{
    /// <summary>
    /// Control that renders a data property from an ontology
    /// </summary>
    public class OntologyDataFieldControl : TextField
    {
        private Dictionary<string, string> listContents = new Dictionary<string, string>();

        /// <summary>
        /// Aspx Label that holds the displayed information
        /// </summary>
        protected System.Web.UI.WebControls.Label valueForDisplay;

        /// <summary>
        /// TODO: Documentation
        /// </summary>
        public IEnumerable<IIndividual> Contents { get; set; }

        /// <summary>
        /// Get the default aspx template name
        /// </summary>
        protected override string DefaultTemplateName
        {
            get
            {
                if (this.ControlMode == SPControlMode.Display)
                {
                    return this.DisplayTemplateName;
                }
                else
                {
                    return "OntologyDataFieldControl";
                }
            }
        }

        /// <summary>
        /// Get the display aspx template name
        /// </summary>
        public override string DisplayTemplateName
        {
            get
            {
                return "OntologyDataFieldControlForDisplay";
            }
            set
            {
                base.DisplayTemplateName = value;
            }
        }

        /// <summary>
        /// Initialize the controls as local variables
        /// </summary>
        protected override void CreateChildControls()
        {
            if (this.Field != null)
            {
                base.CreateChildControls();
                this.textBox = (TextBox)TemplateContainer.FindControl("TextField");
                this.valueForDisplay = (System.Web.UI.WebControls.Label)TemplateContainer.FindControl("ValueForDisplay");

                if (this.ControlMode == SPControlMode.Display)
                {
                    valueForDisplay.Text = (String)this.ItemFieldValue;
                }
            }
        }

        /// <summary>
        /// Get/Set the displayed value
        /// </summary>
        public override object Value
        {
            get
            {
                EnsureChildControls();
                if (!this.Page.IsPostBack)
                {
                    return base.Value;
                }
                else
                {
                    return base.Value;
                }
            }
            set
            {
                EnsureChildControls();
                base.Value = (String)value;
            }
        }
    }
}
